package com.ydkj.a37e_mall.Classes.Acitivity.BaseActivity;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;

import com.ydkj.a37e_mall.Classes.Fragment.AllMallOrderFg;
import com.ydkj.a37e_mall.Classes.Fragment.DealCloseFg;
import com.ydkj.a37e_mall.Classes.Fragment.HadCompleteMallOrderFg;
import com.ydkj.a37e_mall.Classes.Fragment.RefundingMallOrderFg;
import com.ydkj.a37e_mall.Classes.Fragment.ReturningMallOrderFg;
import com.ydkj.a37e_mall.Classes.Fragment.StorePickedupFg;
import com.ydkj.a37e_mall.Classes.Fragment.WaitPayMallOrderFg;
import com.ydkj.a37e_mall.Classes.Fragment.WaitRecipientFg;
import com.ydkj.a37e_mall.R;
import com.ydkj.a37e_mall.Resource.View.StatusBarUtil;

import java.util.ArrayList;

/**
 * Created by Administrator on 2017/1/18.
 * <p>
 * 我的订单
 */

public class MyOrderActivity extends ActionBarActivity {
    ViewPager mViewPager;
    TabsAdapter mTabsAdapter;
    private TextView tv_title;
    private Activity mActivity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        // 构造一个ViewPager，使其成为这个Activity的视图容器
        mViewPager = new ViewPager(this);
        //mViewPager.setId的作用是相当于在布局文件里给viewpager一个id，也就是 android:id="@+id/11";
        mViewPager.setId(R.id.img_back);
        mActivity = this;
        setContentView(mViewPager);
        initWindow();

        // 获取对Actionbar的引用，这种方式兼容android2.1
        final ActionBar bar = getSupportActionBar();
        // 设置Actionbar的模式为 引导Tab的模式，这样，actionbar才会显示出 Tab标签
        bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        bar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_CUSTOM);

        // 如果你只想显示Navigation Tabs，不想显示actionbar上面的标题等布局，你可以调用下面两行代码来实现
        bar.setDisplayShowHomeEnabled(false);

        bar.setDisplayShowTitleEnabled(false);
        bar.setDisplayHomeAsUpEnabled(false);
        bar.setDisplayShowCustomEnabled(true);

//        ViewGroup customView = (ViewGroup) LayoutInflater.from(this).inflate(R.layout.order_top_title, null);
        bar.setDisplayShowCustomEnabled(true);
        bar.setBackgroundDrawable(getResources().getDrawable(R.color.ffffff));
        View actionbarLayout = LayoutInflater.from(this).inflate(R.layout.order_top_title, null);
        ActionBar.LayoutParams layoutParams = new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT,
                ActionBar.LayoutParams.MATCH_PARENT);
        bar.setCustomView(actionbarLayout, layoutParams);
        Toolbar parent = (Toolbar) actionbarLayout.getParent();
        parent.setContentInsetsAbsolute(0, 0);
//        bar.setCustomView(actionbarLayout);
        tv_title = (TextView) bar.getCustomView().findViewById(R.id.tv_title);
        tv_title.setText("我的订单");
        bar.getCustomView().findViewById(R.id.img_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });


        // 构造一个自定义的Adapter，使将Fragment作为一个页面，放入到viewpager中，并且使viewpager和actionbar相关联起来。
        mTabsAdapter = new TabsAdapter(this, mViewPager);

//        mTabsAdapter.addTab(bar.newTab().setText("已付款"), WaitReceiveMallOrderFg.class, null);
//        mTabsAdapter.addTab(bar.newTab().setText("出货中"), WaitReceiveMallOrderFg.class, null);
//        mTabsAdapter.addTab(bar.newTab().setText("已出货"), HadCompleteMallOrderFg.class, null);
//        mTabsAdapter.addTab(bar.newTab().setText("确认收货"), HadCompleteMallOrderFg.class, null);
//        mTabsAdapter.addTab(bar.newTab().setText("已删除"), HadCompleteMallOrderFg.class, null);
//
//        mTabsAdapter.addTab(bar.newTab().setText("待收货"), WaitReceiveMallOrderFg.class, null);


        mTabsAdapter.addTab(bar.newTab().setText("全部"), AllMallOrderFg.class, null);
        mTabsAdapter.addTab(bar.newTab().setText("待付款"), WaitPayMallOrderFg.class, null);
        mTabsAdapter.addTab(bar.newTab().setText("待收货"), WaitRecipientFg.class, null);
        mTabsAdapter.addTab(bar.newTab().setText("已完成"), HadCompleteMallOrderFg.class, null);
//        mTabsAdapter.addTab(bar.newTab().setText("退货中"), ReturningMallOrderFg.class, null);
        mTabsAdapter.addTab(bar.newTab().setText("换货中"), RefundingMallOrderFg.class, null);
        mTabsAdapter.addTab(bar.newTab().setText("门店自提"), StorePickedupFg.class, null);
        mTabsAdapter.addTab(bar.newTab().setText("交易关闭"), DealCloseFg.class, null);

    }


    private void initWindow() {
        StatusBarUtil.setColor(mActivity, getResources().getColor(R.color.A000000), 0);

    }

    /**
     * 自定义一个adapter,FragmentPagerAdapter是一个用来连接ViewPager和Fragment的适配器。
     * 实现ActionBar.TabListener接口 是为了响应Navigation Tabs的点击事件
     * 实现ViewPager.OnPageChangeListener 是为了响应ViewPager的滑屏事件
     */
    public static class TabsAdapter extends FragmentPagerAdapter implements
            ActionBar.TabListener, ViewPager.OnPageChangeListener {
        private final Context mContext;
        private final ActionBar mActionBar;
        private final ViewPager mViewPager;
        private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();

        static final class TabInfo {
            private final Class<?> clss;
            private final Bundle args;

            TabInfo(Class<?> _class, Bundle _args) {
                clss = _class;
                args = _args;
            }
        }

        public TabsAdapter(ActionBarActivity activity, ViewPager pager) {
            super(activity.getSupportFragmentManager());
            mContext = activity;
            mActionBar = activity.getSupportActionBar();
            mViewPager = pager;
            mViewPager.setOffscreenPageLimit(5);    //预加载，设置预加载的数量，否则页卡会被销毁，设置完配合setUserVisibleHint方法，即可完美解决预加载问题
            mViewPager.setAdapter(this);
            mViewPager.setOnPageChangeListener(this);
        }

        public void addTab(ActionBar.Tab tab, Class<?> clss, Bundle args) {
            TabInfo info = new TabInfo(clss, args);
            tab.setTag(info);
            tab.setTabListener(this);
            mTabs.add(info);
            // 将Tab 添加到 Actionbar当中去，这样才会显示出来
            mActionBar.addTab(tab);
            notifyDataSetChanged();
        }

        @Override
        public int getCount() {
            return mTabs.size();
        }

        @Override
        public Fragment getItem(int position) {
            // 为每一个ViewPager的页面 实例化一个Fragment的实体
            TabInfo info = mTabs.get(position);
            return Fragment.instantiate(mContext, info.clss.getName(), info.args);
        }

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        }

        @Override
        public void onPageSelected(int position) {
            // 当viewpager滑动时，actionbar的tab标签也要相应的改变，一一对应
            mActionBar.setSelectedNavigationItem(position);
        }

        @Override
        public void onPageScrollStateChanged(int state) {
        }

        @Override
        public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
            // 当点击选择不同的tab时，viewpager的页面也要相应的改变，使其一一对应
            Object tag = tab.getTag();
            for (int i = 0; i < mTabs.size(); i++) {
                if (mTabs.get(i) == tag) {
                    mViewPager.setCurrentItem(i);
                }
            }
        }

        @Override
        public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
        }

        @Override
        public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
//        getMenuInflater().inflate(R.menu.main, menu);
        return super.onCreateOptionsMenu(menu);
    }
}
